//
//  ViewController.m
//  RoadLine
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 RedFireNoodles. All rights reserved.
//

#import "ViewController.h"
#import "Constants.h"
#import "Singleton.h"
#import "AFNetworkReachabilityManager.h"

@interface ViewController ()
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:imgView];
    imgView.image = [UIImage imageNamed:@"error.png"];
    
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                [Singleton shareSingleton].statu = @"unknown_internet";
                break;
                
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                if (![[Singleton shareSingleton].statu isEqualToString:@"lost_internet"]) {
                    ViewController *errorVC = [[ViewController alloc] init];
                    [self.navigationController pushViewController:errorVC animated:NO];
                    [errorVC release];
                    [Singleton shareSingleton].statu = @"lost_internet";
                }
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                if (![[Singleton shareSingleton].statu isEqualToString:@"mobile_internet"]) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在是手机自带网络,建议切换到WIFI" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];
                    [Singleton shareSingleton].statu = @"mobile_internet";
                }
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                [Singleton shareSingleton].statu = @"wifi_internet";
                break;
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];
    
//    UIButton *refreshBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//    refreshBtn.frame = CGRectMake(90, self.view.frame.size.height / 2 - 20, 110, 40);
//    refreshBtn.layer.borderWidth = 1;
//    refreshBtn.layer.cornerRadius = 5;
//    refreshBtn.backgroundColor = [UIColor clearColor];
//    [refreshBtn setTitle:@"刷新" forState:UIControlStateNormal];
//    [refreshBtn addTarget:self action:@selector(refreshBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:refreshBtn];
    
    [imgView release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshBtnClick:(id)sender {
    if (![[Singleton shareSingleton].statu isEqualToString:@"lost_internet"]) {
        [self.navigationController popViewControllerAnimated:NO];
    }
}

@end
